require(ggplot2)
require(rgdal)
require(ggplot2)
require(scales)
require(plyr)
require(maptools)

italy_box_y <- c(36, 47.5)
italy_box_x <- c(6.1, 19.5)

load('02_03_m5s_meetup_vote_lm_mod.RData')

italy_box_y <- c(36, 47.5)
italy_box_x <- c(6.1, 19.5) 

# Download shapefile from http://www.istat.it/
italy_map_2010 <-  
  readOGR(dsn = "Prov2010_WGS84_g", layer = "Prov2010_WGS84_g", verbose=FALSE)
italy_map_2010 <- 
  spTransform(italy_map_2010, CRS('+proj=longlat +datum=WGS84'))
italy_map_2013 <- 
  readOGR(dsn = "Prov_2013_WGS84_g", layer = "Prov2013_WGS84_g", verbose=FALSE)
italy_map_2013 <- 
  spTransform(italy_map_2013, CRS('+proj=longlat +datum=WGS84'))
italy_map_2014 <- 
  readOGR(dsn = "Prov2014_WGS84_g", layer = "Prov2014_WGS84_g", verbose=FALSE)
italy_map_2014 <- 
  spTransform(italy_map_2014, CRS('+proj=longlat +datum=WGS84'))

italy_map_2010 <-
  merge(italy_map_2010, 
        election2010_m5s_provincia[,c('Ente', 'members', 'votes', 
                                      'votes_ratio', 'members_ratio')],
        by.x = "PROVINCIA", by.y = "Ente" , all.x = TRUE)

italy_map_2013 <-
  merge(italy_map_2013, 
        election2013_m5s_provincia[,c('PROVINCIA', 'members', 'votes', 
                                      'votes_ratio', 'members_ratio')],
        by.x = "PROVINCIA", by.y = "PROVINCIA" , all.x = TRUE)

italy_map_2014 <-
  merge(italy_map_2014, 
        election2014_m5s_provincia[,c('PROVINCIA', 'members', 'votes',
                                      'votes_ratio', 'members_ratio')],
        by.x = "PROVINCIA", by.y = "PROVINCIA" , all.x = TRUE)

# Plot 
italy_map_2010@data$id = rownames(italy_map_2010@data)
italy_map_2010.points = fortify(italy_map_2010, region="id")
italy_map_2010.df = join(italy_map_2010.points, italy_map_2010@data, by="id")

italy_map_2013@data$id = rownames(italy_map_2013@data)
italy_map_2013.points = fortify(italy_map_2013, region="id")
italy_map_2013.df = join(italy_map_2013.points, italy_map_2013@data, by="id")

italy_map_2014@data$id = rownames(italy_map_2014@data)
italy_map_2014.points = fortify(italy_map_2014, region="id")
italy_map_2014.df = join(italy_map_2014.points, italy_map_2014@data, by="id")

require(viridis)
g_vote2010 <- 
  ggplot() + 
  geom_polygon(data=italy_map_2010.df, 
               aes(long,lat, group=group,fill=votes_ratio)) +
  scale_fill_viridis(labels = scales::percent) +
  scale_x_continuous(limits = c(italy_box_x[1], italy_box_x[2])) +
  scale_y_continuous(limits = c(italy_box_y[1], italy_box_y[2])) +
  labs(x=NULL,y=NULL,fill = 'Votes') +
  theme(axis.text=element_blank(),axis.ticks=element_blank(),
        legend.position = c(0.8, 0.8))

g_member2010 <- 
  ggplot() + 
  geom_polygon(data=italy_map_2010.df, 
               aes(long,lat, group=group,fill=members_ratio)) +
  scale_fill_viridis(labels = scales::percent, option = "C") +
  scale_x_continuous(limits = c(italy_box_x[1], italy_box_x[2])) +
  scale_y_continuous(limits = c(italy_box_y[1], italy_box_y[2])) +
  labs(x=NULL,y=NULL,fill = 'Members') +
  theme(axis.text=element_blank(),axis.ticks=element_blank(),
        legend.position = c(0.8, 0.8))

g_vote2013 <- 
  ggplot() + 
  geom_polygon(data=italy_map_2013.df, 
               aes(long,lat, group=group,fill=votes_ratio)) +
  scale_fill_viridis(labels = scales::percent) +
  scale_x_continuous(limits = c(italy_box_x[1], italy_box_x[2])) +
  scale_y_continuous(limits = c(italy_box_y[1], italy_box_y[2])) +
  labs(x=NULL,y=NULL,fill = 'Votes') +
  theme(axis.text=element_blank(),axis.ticks=element_blank(),
        legend.position = c(0.8, 0.8))

g_member2013 <- 
  ggplot() + 
  geom_polygon(data=italy_map_2013.df, 
               aes(long,lat, group=group,fill=members_ratio)) +
  scale_fill_viridis(labels = scales::percent, option = "C") +
  scale_x_continuous(limits = c(italy_box_x[1], italy_box_x[2])) +
  scale_y_continuous(limits = c(italy_box_y[1], italy_box_y[2])) +
  labs(x=NULL,y=NULL,fill = 'Members') +
  theme(axis.text=element_blank(),axis.ticks=element_blank(),
        legend.position = c(0.8, 0.8))

g_vote2014 <- 
  ggplot() + 
  geom_polygon(data=italy_map_2014.df, 
               aes(long,lat, group=group,fill=votes_ratio)) +
  scale_fill_viridis(labels = scales::percent) +
  scale_x_continuous(limits = c(italy_box_x[1], italy_box_x[2])) +
  scale_y_continuous(limits = c(italy_box_y[1], italy_box_y[2])) +
  labs(x=NULL,y=NULL,fill = 'Votes') +
  theme(axis.text=element_blank(),axis.ticks=element_blank(),
        legend.position = c(0.8, 0.8))

g_member2014 <- 
  ggplot() + 
  geom_polygon(data=italy_map_2014.df, 
               aes(long,lat, group=group,fill=members_ratio)) +
  scale_fill_viridis(labels = scales::percent, option = "C") +
  scale_x_continuous(limits = c(italy_box_x[1], italy_box_x[2])) +
  scale_y_continuous(limits = c(italy_box_y[1], italy_box_y[2])) +
  labs(x=NULL,y=NULL,fill = 'Members') +
  theme(axis.text=element_blank(),axis.ticks=element_blank(),
        legend.position = c(0.8, 0.8))

